package com.awspaass.user.apps.syncwechataddress;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.awspaass.user.apps.syncwechataddress.WxUserInfo;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class WeChatUtil {

	public String getAccessToken(String corpid, String corpsecret) {
		String access_token;

//		String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0605aae701a55d9e&corpsecret=ZwrE6zsNaSfu_OdShAm1OzvJz0CvK-AZnVho9lPOMQU";
		String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret="
				+ corpsecret;
		String result = HttpClientUtil.doGet(access_token_url);

		JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
		// System.out.println(jsonObject);

		access_token = jsonObject.get("access_token").getAsString();
		// System.out.print(access_token);

		return access_token;
	}

	/**
	 * 获取部门列表
	 * 
	 * @param accessToken
	 * @param departmentId
	 * @return
	 */
	public List<Map<String, String>> getDepartmentList(String accessToken) {
		List<Map<String, String>> departments = new ArrayList<Map<String, String>>();
		// 1.获取请求的url
		String getDepartmentList_url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="
				+ accessToken;
		// 2.调用接口，发送请求，获取成员
		JsonObject jsonObject = new JsonParser().parse(HttpClientUtil.doGet(getDepartmentList_url)).getAsJsonObject();

		// System.out.println("jsonObject:" + jsonObject.toString());
		// 3.错误消息处理
		if (null != jsonObject) {
// 查询成功
			List<Map<String, Object>> mapListJson = new Gson().fromJson(jsonObject.get("department").getAsJsonArray(),
					new TypeToken<List<Map<String, Object>>>() {
					}.getType());
			// List<Map<String, Object>> mapListJson
			// =(List)jsonObject.get("department").getAsJsonArray();

			if (null != mapListJson) {
				// Department department = new Department();
				for (int i = 0; i < mapListJson.size(); i++) {
					Map<String, Object> dept = mapListJson.get(i);
					if (null != dept.get("id")) {
						Integer id = ((Double) dept.get("id")).intValue();
						Integer p_id = ((Double) dept.get("parentid")).intValue();
						String name = (String) dept.get("name");
						departments.add(new HashMap<String, String>() {
							{
								put("id", id.toString());
							}
						});
						departments.add(new HashMap<String, String>() {
							{
								put("name", name);
							}
						});
						departments.add(new HashMap<String, String>() {
							{
								put("parentid", p_id.toString());
							}
						});

					}

				}
			}

		}
		System.out.println(departments);
		return departments;
	}

	/**
	 * 获取部门成员详情
	 * 
	 * @param depts
	 * @param accessToken
	 * @param fetchChild//是否遍历子部门的成员，一般不要遍历，除非你就只获取父级部门或者子部门为空，不然会导致数据重复
	 * @return
	 */
	public List<WxUserInfo> getDepartmentUserDetails(List<Map<String, String>> depts, String accessToken,
			String fetchChild) {
		List<WxUserInfo> users = new ArrayList<WxUserInfo>();
		int count = 0;
		// System.out.println(depts.get(0));
		for (count = 0; count < depts.size(); count++) {
			// 1.获取请求的url
			Map<String, String> depid = depts.get(count);
			count++;
			Map<String, String> depname = depts.get(count);
			count++;
			// System.out.println(depname.get("name"));
			String getDepartmentUserDetails_url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token="
					+ accessToken + "&department_id=" + depid.get("id") + "&fetch_child=" + fetchChild;

			// 2.调用接口，发送请求，获取部门成员
			JsonObject jsonObject = new JsonParser().parse(HttpClientUtil.doGet(getDepartmentUserDetails_url))
					.getAsJsonObject();
			// System.out.println(jsonObject);
			// 3.错误消息处理
			if (null != jsonObject) {

				// JSONArray array=jsonObject.getJSONArray("userlist");
				List<Map<String, Object>> mapListJson = new Gson().fromJson(jsonObject.get("userlist").getAsJsonArray(),
						new TypeToken<List<Map<String, Object>>>() {
						}.getType());

				if (null != mapListJson) {
					for (int i = 0; i < mapListJson.size(); i++) {
						Map<String, Object> de = mapListJson.get(i);
						// 具体字段看自己业务需求
						WxUserInfo user = new WxUserInfo();
						user.setUserId(String.valueOf(de.get("userid")));
						user.setDepartmentName(String.valueOf(depname.get("name")));
						user.setDepartmentId(depid.get("id"));
						user.setName(String.valueOf(de.get("name")));
						user.setPosition(String.valueOf(de.get("position")));
						user.setMobile(String.valueOf(de.get("mobile")));
						user.setGender(String.valueOf(de.get("gender")));
						user.setEmail(String.valueOf(de.get("email")));
						user.setStatus(String.valueOf(de.get("status")));
						user.setAlias(String.valueOf(de.get("alias")));
						// System.out.println(user.getName());

						users.add(user);
					}
				}
			}

		}

		return users;
	}

	public String getDKToken(String corpid, String cksecret) {
		String ss = null;
		try {
			// 当前时间戳
			String generalUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; // Token地址
			URL url = new URL(generalUrl);
			String strRead = null;
			StringBuffer sbf = new StringBuffer();
			// 连接接口路径
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			// 设置请求属性
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setUseCaches(false);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			// 建立连接
			connection.connect();
			OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
			// 插入Body里面的json数据
			JSONObject parm = new JSONObject();
			parm.put("corpid", corpid); // AppKey
			parm.put("corpsecret", cksecret);// AppSecre
			writer.write(parm.toString());
			writer.flush();
			InputStream is = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close(); // 关流
			connection.disconnect();
			ss = JSON.parseObject(sbf.toString()).getString("access_token"); // 获取data对象
			if ("".equals(ss) || ss == null) {
				System.out.println("获取token失败>>>>>ss的值是：" + ss);
			}
			// 返回token字符串

		} catch (Exception e) {
			e.printStackTrace();
		}

		return ss;
	}

	public int getDKData(String corpid, String cksecret, List userlist) {
		int number = 0;
		try {
			SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			String date=datef.format(new Date());
		    String begintime=date+" 00:00";
		    String endtime=date+" 08:30";
		    SimpleDateFormat datetimef = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		    Date start=datetimef.parse(begintime); 
		    Date end=datetimef.parse(endtime);
			long startT = start.getTime()/1000;// 开始时间
			long endT = end.getTime()/1000;// 结束时间
			String generalUrl = " https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token="
					+ getDKToken(corpid, cksecret) + ""; // Token地址
			URL url = new URL(generalUrl);
			String strRead = null;
			StringBuffer sbf = new StringBuffer();
			// 连接接口路径
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			// 设置请求属性
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setUseCaches(false);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			// 建立连接
			connection.connect();
			OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
			// 插入Body里面的json数据
			JSONObject parm = new JSONObject();
			parm.put("opencheckindatatype", "3"); // AppKey
			parm.put("starttime", startT); // AppKey
			parm.put("endtime", endT);// AppSecre
			// System.out.println(userlist);
			parm.put("useridlist", userlist);// AppSecre
			writer.write(parm.toString());
			writer.flush();
			InputStream is = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close(); // 关流
			connection.disconnect();
			JSONArray j = JSON.parseObject(sbf.toString()).getJSONArray("checkindata");
			// System.out.println(j.size());
			// System.out.println(sbf.toString());
			// return JSONObject.parseObject(sbf.toString());// .getString("checkindata");
			number = j.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return number;
	}

	public String readToString(String fileName) {
		String encoding = "UTF-8";
		File file = new File(fileName);
		Long filelength = file.length();
		byte[] filecontent = new byte[filelength.intValue()];
		try {
			FileInputStream in = new FileInputStream(file);
			in.read(filecontent);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			return new String(filecontent, encoding);
		} catch (UnsupportedEncodingException e) {
			System.err.println("The OS does not support " + encoding);
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		/*
		 * WeChatUtil a = new WeChatUtil(); String
		 * access_token=a.getAccessToken("wwd1feef53690aecf0",
		 * "tBQRUDNSAIEKx_HwkqTLXjSwr_0ifr_o2vdsOc1_aU8"); List<Map<String,String>> deps
		 * =a.getDepartmentList(access_token);
		 * 
		 * a.getDepartmentUserDetails(deps, access_token, "0");
		 */
	}

}
